package com.qqzone.qq.Controller;

import com.qqzone.qq.pojo.Topic;
import com.qqzone.qq.pojo.UserBasic;
import com.qqzone.qq.service.TopicService;

import javax.servlet.http.HttpSession;
import java.util.List;

public class TopicController {

    private TopicService topicService;

    public String topicDetail(Integer id, HttpSession session) {

        Topic topic = topicService.getTopicById(id);
        session.setAttribute("topic", topic);
        return "frames/detail";
    }

    public String getTopicList(HttpSession session) {
        //从session中获取当前用户信息
        Object userBasic = session.getAttribute("friend");
        //再次查询当前用户关联的所有的日志
        List<Topic> topicList = topicService.getTopicList((UserBasic) userBasic);
        //设置一下关联的日志列表(因为之前session中关联的friend的topicList和此刻数据库中不一致）
        ((UserBasic) userBasic).setTopicList(topicList);
        //重新覆盖一下friend中的信息(为什么不覆盖userbasic中？因为main.html页面迭代的是friend这个key中的数据)
        session.setAttribute("friend", userBasic);

        return "frames/main";
    }

    public String delTopic(Integer topicId) {
        topicService.delTopicById(topicId);
        return "redirect:topic.do?operate=getTopicList";
    }
}
